import { http } from "@/utils/request";
import type { WXLoginParameters, ResWXLoginInfo } from "./types/authType";

// 获取微信登录参数
export function getWxLoginParameters() {
  return http.get<WXLoginParameters>("/wechat/getLoginParameters");
}

/**
 * 获取微信登录凭证
 * @returns Promise 包含微信登录凭证(code)
 */
export function getWxCode() {
  return new Promise<UniApp.LoginRes>((resolve, reject) => {
    uni.login({
      provider: "weixin",
      success: (res) => resolve(res),
      fail: (err) => reject(new Error(err)),
    });
  });
}

/**
 * 微信登录
 * @param params 微信登录参数，包含code
 * @returns Promise 包含登录结果
 */
export function wxLogin(data: { code: string }) {
  return http.post<ResWXLoginInfo>("/miniApp/getOpenId", data);
}

/**
 * @description: 未绑定微信登录
 * @param {object} data
 * @return {*}
 */
export function wxLoginByPhone(data: { code: string; openId: string }) {
  return http.post<ResWXLoginInfo>("/miniApp/wechatLogin", data);
}

// 登出接口
export const logout = () => {
  return http.get<void>("/logout");
};
